/*******************************************************************************
 *	                           bsp spi.c
 *******************************************************************************/
#include "bsp.h"
#include "bsp_spi_nor.h"
#include "pincfg.h"
#include "nor_cmd.h"
#include "framework.h"

/*****************************************************************************
 * @brief   SPI
 *****************************************************************************/
#define NOR_SPI SPI1
#define ISR_IRQn SPI1_IRQn

/*****************************************************************************
 * @brief   Var
 *****************************************************************************/
uint8_t b_io_q_mode = 0;
const spi_flash_hal_type spi_nor;

/*****************************************************************************
 * @brief   hal init.
 *
 * @param   none
 *
 * @return  none
 *
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void nor_spi_init(void)
{
	SPI_Config_T cfg;
	intx_alloc();

	// 开启时钟
	intx_disable();

	RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_SPI1);
	RCM_EnableAPB2PeriphReset(RCM_APB2_PERIPH_SPI1);
	RCM_DisableAPB2PeriphReset(RCM_APB2_PERIPH_SPI1);

	intx_enable();

	// SPI 配置
	cfg.direction = SPI_DIRECTION_2LINES_FULLDUPLEX;
	cfg.baudrateDiv = SPI_BAUDRATE_DIV_16;
	cfg.mode = SPI_MODE_MASTER;
	cfg.length = SPI_DATA_LENGTH_8B;
	cfg.firstBit = SPI_FIRSTBIT_MSB;
	cfg.nss = SPI_NSS_SOFT;
	cfg.polarity = SPI_CLKPOL_LOW;
	cfg.phase = SPI_CLKPHA_1EDGE;
	cfg.crcPolynomial = 0;

	SPI_Config(NOR_SPI, &cfg);

	NOR_SPI->CTRL1_B.SPIEN = 1;

	nor_spi_cs(0);

	nor_spi_rb();

	nor_spi_cs(1);

	b_io_q_mode = 0;

	// 接口设置
	nor_init(&spi_nor, 0);
}

/*****************************************************************************
 * @brief   cs ctr.
 * @param   st
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void nor_spi_cs(uint8_t st)
{
	// CONFIG :: SPI_NSS_HARD_OUTPUT
	if (st)
	{
		// cs out high @ SPI disabled
		//snor_ss.en();
		NOP20();
	}
	else
	{
		// cs out low @ SPI enabled
		//snor_ss.dis();
		NOP20();
	}
}

/*****************************************************************************
 * @brief   spi mode select.
 *****************************************************************************/
void nor_hal_mode_spi(void) { b_io_q_mode = 0; };
void nor_hal_mode_qpi_out(void) { b_io_q_mode = 1; };
void nor_hal_mode_qpi_in(void) { b_io_q_mode = 1; };

/*****************************************************************************
 * @brief   spi speed.
 *****************************************************************************/
void nor_spi_high_speed(void)
{
}

/*****************************************************************************
 * @brief   spi speed.
 *****************************************************************************/
void nor_spi_low_speed(void)
{
}

/*****************************************************************************
 * @brief   spi delay.
 *****************************************************************************/
#define spi_read_delay() NOP8()
#define is_spi_rx_ready(spix) ((spix->STS_B.RXBNEFLG) != 0)
#define spi_rx_reset(spix)                 \
	while (is_spi_rx_ready(spix))          \
	{                                      \
		volatile uint32_t rd = spix->DATA; \
	}
#define is_spi_busy(spix) (spix->STS_B.BSYFLG)
#define spi_send(spix, dat) spix->DATA = dat
#define is_spi_tx_full(spix) ((spix->STS_B.TXBEFLG) == 0)
#define is_spi_tx_empty(spix) (spix->STS_B.TXBEFLG)
#define spi_read(spix) spix->DATA

/*****************************************************************************
 * @brief   spi data read.
 *****************************************************************************/
uint8_t nor_spi_rb(void)
{
	// send data
	spi_send(NOR_SPI, 0xFF);
	spi_read_delay();

	// wait for trans complete
	while (!is_spi_rx_ready(NOR_SPI))
		;

	// read data
	return (spi_read(NOR_SPI));
}

void nor_spi_wb(uint8_t dataW)
{
	volatile uint8_t rb;

	// send data
	spi_send(NOR_SPI, dataW);
	spi_read_delay();

	// wait for trans complete
	while (!is_spi_rx_ready(NOR_SPI))
		;

	// read data
	rb = spi_read(NOR_SPI);
}

/*****************************************************************************
 * @brief   spi data write.
 *****************************************************************************/
uint8_t nor_spi_write(const uint8_t *dataw, uint32_t Len)
{
	while (Len--)
	{
		// send data
		spi_send(NOR_SPI, *dataw++);

		spi_read_delay();

		while (is_spi_tx_full(NOR_SPI))
		{
		}
	}

	spi_read_delay();

	// wait for trans complete
	while (is_spi_busy(NOR_SPI) || !is_spi_tx_empty(NOR_SPI))
	{
	}

	spi_rx_reset(NOR_SPI);

	return (0);
}

/*****************************************************************************
 * @brief   spi data read.
 *****************************************************************************/
uint8_t nor_spi_read(uint8_t *datar, uint32_t Len)
{
	uint32_t rcnt = 0;

	while (Len--)
	{
		// send data
		spi_send(NOR_SPI, 0xFF);

		spi_read_delay();

		while (!is_spi_rx_ready(NOR_SPI))
		{
		}

		datar[rcnt++] = spi_read(NOR_SPI);
	}

	return 0;
}

/*****************************************************************************
 * @brief   qpi data write.
 *****************************************************************************/
uint8_t nor_qpi_write(const uint8_t *dataw, uint32_t Len)
{
	return (0);
}

/*****************************************************************************
 * @brief   qpi data read.
 *****************************************************************************/
uint8_t nor_qpi_read(uint8_t *datar, uint32_t Len)
{
	return (0);
}

/*****************************************************************************
 * @brief   nor spi
 *****************************************************************************/
const spi_flash_hal_type spi_nor = {
	//* extern api
	.cs = nor_spi_cs,
	.mode_spi = nor_hal_mode_spi,
	.mode_qpi_out = nor_hal_mode_qpi_out,
	.mode_qpi_in = nor_hal_mode_qpi_in,
	.spi_rb = nor_spi_rb,
	.spi_wb = nor_spi_wb,
	.spi_write = nor_spi_write,
	.spi_read = nor_spi_read,
	.qpi_write = nor_qpi_write,
	.qpi_read = nor_qpi_read,
};

/*****************************************************************************
 * @brief   falsh api.
 * @param   st
 * @return  none
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
uint8_t ext_flash_sector_erase(uint32_t addr)
{
	uint8_t st;

	osMutexWait(os_obj.mid_q25, INT32_MAX);

	st = nor_sector_erase(addr);

	osMutexRelease(os_obj.mid_q25);

	return st;
}

uint8_t ext_flash_block_erase(uint32_t addr)
{
	uint8_t st;

	osMutexWait(os_obj.mid_q25, INT32_MAX);

	st = nor_block_erase(addr);

	osMutexRelease(os_obj.mid_q25);

	return st;
}

uint8_t ext_flash_read(uint32_t addr, int32_t Len, uint8_t *Des)
{
	uint8_t st;

	osMutexWait(os_obj.mid_q25, INT32_MAX);

	st = nor_read(addr, Len, Des);

	osMutexRelease(os_obj.mid_q25);

	return st;
}

uint8_t ext_flash_write_multi(uint32_t addr, int32_t Len, const uint8_t *Src)
{
	uint8_t st;

	osMutexWait(os_obj.mid_q25, INT32_MAX);

	st = nor_write_multi(addr, Len, Src);

	osMutexRelease(os_obj.mid_q25);

	return st;
}
